package com.smarthome.core.advicemanager.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShPlatformNoticeService;
import com.smarthome.domain.enums.AdviceStatusEnum;
import com.smarthome.common.tools.EnumUtil;
import com.smarthome.core.advicemanager.PlatformAdviceService;
import com.smarthome.domain.bean.ShPlatformNotice;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * 
* @ClassName: PlatformAdviceServiceImpl
* @Description: 平台公告管理服务接口实现类
* @author cyj
* @date 2015年9月23日 下午3:36:57
*
 */
@Service("platformAdviceService")
public class PlatformAdviceServiceImpl implements PlatformAdviceService {
	
	@Resource
	private ShPlatformNoticeService platformNoticeService;

	@Override
	public ShPlatformNotice queryShPlatformNoticeById(Long id) {
		return platformNoticeService.selectEntryById(id);
	}


	@Override
	public DataGrid qeuryDataGrid(ShPlatformNotice platformNotice, PageHelper page) {
		return platformNoticeService.quetyDataGrid(platformNotice, page);
	}


	@Override
	public int add(ShPlatformNotice platformNotice) {
		return platformNoticeService.saveOrUpdate(platformNotice);
	}


	@Override
	public int delete(Long id) {
		return platformNoticeService.deleteByIds(id);
	}


	@Override
	public List<Combobox> status(boolean flag) {
		List<Combobox> result = new ArrayList<>();
		if(flag) {
			Combobox combobox = new Combobox();
			combobox.setValue("");
			combobox.setText("--请选择--");
			combobox.setSelected(true);
			result.add(combobox);
		}
		List<AdviceStatusEnum> statusEnum = EnumUtil.getEnumList(AdviceStatusEnum.class);
		for(AdviceStatusEnum status:statusEnum) {
			result.add(new Combobox(String.valueOf(status.getCode()),status.getName()));
		}
		return result;
	}


	@Override
	public int publish(ShPlatformNotice platformNotice) {
		ShPlatformNotice savePlatformNotice = platformNoticeService.selectEntryById(platformNotice.getId());
		if(savePlatformNotice != null) {
			savePlatformNotice.setPublishTime(new Date());
			savePlatformNotice.setStatus(AdviceStatusEnum.PUBLISHED.getCode());
			return platformNoticeService.updateById(savePlatformNotice);
		}
		return 0;
	}


	@Override
	public ShPlatformNotice getById(Long id) {
		return platformNoticeService.selectEntryById(id);
	}


}
